package main.java.com.ThinkInJava.Chapter21并发._21_5_3生产者与消费者;

public class WaitPersion implements Runnable {
    private Restaurant restaurant;
    public WaitPersion(Restaurant r) {
        restaurant = r;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (restaurant.meal == null) {
                        wait();
                    }
                    System.out.println("Waitperson got " + restaurant.meal);
                    synchronized (restaurant.chef) {
                        restaurant.meal = null;
                        restaurant.chef.notifyAll();
                    }
                }
            }
        } catch (InterruptedException e) {
            System.out.println("WaitPerson interrupt");
            e.printStackTrace();
        }
    }
}
